package cn.tedu.system.oa.notice.startup;

import cn.tedu.system.oa.notice.pojo.entity.Notice;
import cn.tedu.system.oa.notice.service.NoticeService;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class NoticeCacheInit implements ApplicationRunner {

    @Autowired
    private NoticeService noticeService;
    
    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<Notice> list = checkNoticeList();
    }
    
    private List<Notice> checkNoticeList(){
        log.debug("缓存预热:查询公告列表");
        List<Notice> noticeList =noticeService.getAllNotice();
        log.debug("缓存预热:查询公告列表,结果:{}", noticeList);
        
        if (CollectionUtils.isEmpty(noticeList)){
            log.debug("缓存预热:查询公告列表结果为空,从数据库中查询");
            List<Notice> list = noticeService.selectNotice(null);
            log.debug("存入缓存成功:{}",list);
            return list;
        }
        log.debug("缓存预热:查询公告列表,结果不为空,直接返回:{}", noticeList);
        return noticeList;
    }
}
